package com.hotel.po.mapper;

import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class HotelMapper {

    public List<Map<String, String>> findAll() {
        String sql = "SELECT id, name, address, tel, star, price_avg AS priceAvg, image_url AS imageUrl, description, created_at AS createdAt FROM hotel ORDER BY id DESC";
        return DBTools.queryWithBusiness(sql);
    }
    
    public List<Map<String, String>> search(String keyword) {
        String sql = "SELECT id, name, address, tel, star, price_avg AS priceAvg, image_url AS imageUrl, description, created_at AS createdAt FROM hotel WHERE name LIKE ? OR address LIKE ? OR description LIKE ? ORDER BY id DESC";
        String searchPattern = "%" + keyword + "%";
        return DBTools.queryWithBusiness(sql, searchPattern, searchPattern, searchPattern);
    }

    public Long insert(Map<String, Object> body) {
        String sql = "INSERT INTO hotel(name,address,tel,star,price_avg,image_url,description) VALUES(?,?,?,?,?,?,?)";
        String name = (String) body.getOrDefault("name", "");
        String address = (String) body.getOrDefault("address", "");
        String tel = (String) body.getOrDefault("tel", null);
        Integer star = body.get("star") == null ? 3 : Integer.parseInt(body.get("star").toString());
        Double priceAvg = body.get("priceAvg") == null ? 0D : Double.parseDouble(body.get("priceAvg").toString());
        String imageUrl = (String) body.getOrDefault("imageUrl", null);
        String description = (String) body.getOrDefault("description", null);
        DBTools.updateWithBusiness(sql, name, address, tel, star, priceAvg, imageUrl, description);
        String id = DBTools.querySingle("SELECT LAST_INSERT_ID() AS id");
        return Long.valueOf(id);
    }

    public int update(Map<String, Object> body) {
        String sql = "UPDATE hotel SET name=?, address=?, tel=?, star=?, price_avg=?, image_url=?, description=? WHERE id=?";
        Long id = Long.parseLong(body.get("id").toString());
        String name = (String) body.getOrDefault("name", "");
        String address = (String) body.getOrDefault("address", "");
        String tel = (String) body.getOrDefault("tel", null);
        Integer star = body.get("star") == null ? 3 : Integer.parseInt(body.get("star").toString());
        Double priceAvg = body.get("priceAvg") == null ? 0D : Double.parseDouble(body.get("priceAvg").toString());
        String imageUrl = (String) body.getOrDefault("imageUrl", null);
        String description = (String) body.getOrDefault("description", null);
        return DBTools.updateWithBusiness(sql, name, address, tel, star, priceAvg, imageUrl, description, id);
    }

    public int delete(Long id) {
        return DBTools.updateWithBusiness("DELETE FROM hotel WHERE id=?", id);
    }

    public Map<String, String> findById(Long id) {
        String sql = "SELECT id, name, address, tel, star, price_avg AS priceAvg, image_url AS imageUrl, description, created_at AS createdAt FROM hotel WHERE id=?";
        List<Map<String, String>> list = DBTools.queryWithBusiness(sql, id);
        return list == null || list.isEmpty() ? null : list.get(0);
    }
}


